<?php
include_once('Publication.php');
include_once('Author.php');
include_once('calcHindex.php');

if(isset($_GET['u1'])) {
   
    
    $uIDArray = array();
    $uID = 'u1';
    $uIDcount = 1;
    
    while (isset($_GET[$uID])) 
    {
        array_push($uIDArray, $_GET[$uID]);
        $uIDcount++;
        $uID = "u" . $uIDcount;
        
    }
    
    $domainID = $_GET['domainID'];
    $subDomainID = $_GET['subDomainID'];
    $yearStart = intval($_GET['yearStart']);
    $yearEnd = intval($_GET['yearEnd']);
ini_set('max_execution_time', 300);





$results = array();
$yearArray = array();


foreach ($uIDArray as $u) 
{
    $schoolArray = array();
    $fundingArray = getFunding($u);
    for ($i=$yearStart; $i<=$yearEnd; $i++)
   {
        $pub = new Publication;
        //reduce size of query
        $pub->pubContent = "MetaOnly";
        
        $author = new Author;
        
        if ($domainID !=0) 
        {
            $pub->domainID = $domainID;
            $author->domainID = $domainID;
        }
   
        
        if ($subDomainID !=0) 
        {
            $pub->subDomainID = $subDomainID;
            $author->subDomainID = $subDomainID;
        }
        
        $pub->orgID = $u;
        $author->orgID = $u; 
        
        $pub->yearStart=$i;
        $pub->yearEnd=$i;
    
        $author->yearStart=$i;
        $author->yearEnd=$i;
        
        $pub->makeQuery();
        $author->getAuthors();
        
        $numAuthors = intval($author->getAuthorCount());
        if ($numAuthors == 0) 
        {
            $numAuthors = 1;
        }
   
        $normalized = intval($pub->getPublicationCount())/$numAuthors;
        
          
        $firstSchool = new schoolInfo;
   
        $firstSchool->normalizedAuthor = $normalized;
        $firstSchool->authorCount = $numAuthors;
        $firstSchool->totalPub = $pub->getPublicationCount();
        $firstSchool->hIdx = calcHindex($u, $i, $i, $domainID, $subDomainID);
        if (array_key_exists((string) $i, $fundingArray)) 
        {
            $firstSchool->funding = $fundingArray[(string) $i];
        }
        else 
        {
            $firstSchool->funding = 1;
        }
        
        
        array_push($schoolArray, $firstSchool);
   }
   
     array_push($results, $schoolArray);
}
    





for ($i=$yearStart; $i<=$yearEnd; $i++)
{

    array_push($yearArray, $i);
    
    
}

    $jsonObj = new jsonObj;
    $jsonObj->schools = $results; 
    
    $jsonObj->yearArray = $yearArray;


echo json_encode($jsonObj);


}

function getFunding($orgID) 
{
 $db = mysql_connect('localhost', 'boughtns_user', 'GodLovesYou!');
    
if (!$db) {
    die('Could not connect: ' . mysql_error());
 }
    
mysql_select_db('boughtns_mas', $db);

    $query = "SELECT year, amount FROM funding where university_id = " . $orgID;
   
    $result = mysql_query($query);
    
    $fundingArray = array();
   
    while ($row = mysql_fetch_assoc($result)) 
    {
        $year = $row['year'];
        $amount = $row['amount'];
            
        $fundingArray[$year] = $amount;     
       
    }
    mysql_close($db);    
    return $fundingArray;
    
}

class jsonObj 
{
    public $schools, $yearArray;
}

class schoolInfo
{
    public $normalizedAuthor, $authorCount, $totalPub, $hIdx, $funding;
}

?>